package com.pan.admin.module.api.controller;

import com.github.pagehelper.PageHelper;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.PageDomain;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.core.page.TableSupport;
import com.pan.admin.common.utils.PageUtils;
import com.pan.admin.module.api.entity.ApiLog;
import com.pan.admin.module.api.entity.qo.ApiLogQO;
import com.pan.admin.module.api.service.ApiKeyService;
import com.pan.admin.module.api.service.ApiLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * API日志
 *
 * @author caoxin
 */
@RestController
public class ApiLogController extends BaseController {

    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private ApiKeyService apiKeyService;

    /**
     * api渠道
     */
    @GetMapping("/api/log/listApiKey")
    @PreAuthorize("@ss.hasPermi('api:log:list')")
    public AjaxResult listApiKey(@RequestParam(required = false) String keyword) {
        return AjaxResult.success(this.apiKeyService.listApiKey(keyword));
    }

    /**
     * 查询API日志列表
     */
    @GetMapping("/api/log/list")
    @PreAuthorize("@ss.hasPermi('api:log:list')")
    public TableDataInfo list(ApiLogQO apiLogQO) {
        //  解决数据量大情况
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        PageUtils.startPage(pageNum, pageSize, true);
        // 分页不执行count语句查询总行数
        PageHelper.getLocalPage().count(false);
        List<ApiLog> list = this.apiLogService.list(apiLogQO);
        return getDataTable(list);
    }

    /**
     * 获取API日志详细信息
     */
    @GetMapping(value = "/api/log/{id}")
    @PreAuthorize("@ss.hasPermi('api:log:query')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(this.apiLogService.getById(id));
    }
}
